home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / dviware / umddvi / lib / getopt.c < prev    next >
C/C++ Source or Header  |  1990-10-01  |  1KB  |  69 lines

  1. /*
  2.  * Copyright (c) 1987 University of Maryland Department of Computer Science.
  3.  * All rights reserved.  Permission to copy for any purpose is hereby granted
  4.  * so long as this copyright notice remains intact.
  5.  */
  6.  
  7. /*
  8.  * getopt - get option letter from argv
  9.  * (From Henry Spencer @ U of Toronto Zoology, slightly edited)
  10.  */
  11.  
  12. #include <stdio.h>
  13.  
  14. char    *optarg;    /* Global argument pointer. */
  15. int    optind;        /* Global argv index. */
  16.  
  17. static char *scan;    /* Private scan pointer. */
  18.  
  19. extern char *index();
  20.  
  21. int
  22. getopt(argc, argv, optstring)
  23.     register int argc;
  24.     register char **argv;
  25.     char *optstring;
  26. {
  27.     register int c;
  28.     register char *place;
  29.  
  30.     optarg = NULL;
  31.     if (scan == NULL || *scan == 0) {
  32.         if (optind == 0)
  33.             optind++;
  34.         if (optind >= argc || argv[optind][0] != '-' ||
  35.             argv[optind][1] == 0)
  36.             return (EOF);
  37.         if (strcmp(argv[optind], "--") == 0) {
  38.             optind++;
  39.             return (EOF);
  40.         }
  41.         scan = argv[optind] + 1;
  42.         optind++;
  43.     }
  44.     c = *scan++;
  45.     place = index(optstring, c);
  46.  
  47.     if (place == NULL || c == ':') {
  48.         fprintf(stderr, "%s: unknown option -%c\n", argv[0], c);
  49.         return ('?');
  50.     }
  51.     place++;
  52.     if (*place == ':') {
  53.         if (*scan != '\0') {
  54.             optarg = scan;
  55.             scan = NULL;
  56.         } else {
  57.             if (optind >= argc) {
  58.                 fprintf(stderr,
  59.                     "%s: missing argument after -%c\n",
  60.                     argv[0], c);
  61.                 return ('?');
  62.             }
  63.             optarg = argv[optind];
  64.             optind++;
  65.         }
  66.     }
  67.     return (c);
  68. }
  69.